#pragma once

#include <iostream>
#include <string>
#include<unistd.h>
#include"Init.hpp"

Init init;

using namespace std;


class Task
{
public:
    Task(int sockfd,uint16_t port,string ip)
        :sockfd(sockfd)
        ,clientport(port)
        ,clientip(ip)
    {
    }
    void run()
    {
        //短服务
        char buffer[4096];
        //接受消息
        ssize_t n = read(sockfd,buffer,sizeof(buffer));
        if(n > 0)
        {
            buffer[n] = 0;
            std::cout << "Clinet say# " << buffer << std::endl;

            // std::string echo_string = "TcpServer echo# ";
            // echo_string += buffer;
            std::string echo_string = init.Translation(buffer);

            //返回消息
            n = write(sockfd,echo_string.c_str(),echo_string.size());
            if(n < 0)
            {
                std::cerr << "write error " << std::endl;
            }
        }
        else if(n == 0)
        {
            std::cout << "client shut down ,clientip:" << clientip << " ,clientport:" << clientport << std::endl;
            //break; 
        }
        else
        {
            std::cout << "read failed" << std::endl;
            //break;
        }
        close(sockfd);
    }


    ~Task()
    {}

private:
    int sockfd;
    uint16_t clientport;
    string clientip;
};